//
//  NetEasyContentViewController.m
//  Together
//
//  Created by Wang Shuguang on 12-12-17.
//  Copyright (c) 2012年 Dawn. All rights reserved.
//

#import "NetEasyContentViewController.h"
#import "JSONKit.h"

@interface NetEasyContentViewController ()

@end

@implementation NetEasyContentViewController


- (id) initWithURL:(NSString *)url docid:(NSString *)docid{
    if (self = [super initWithNibName:nil bundle:nil]) {
        contentView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, MAINCONTENTHEIGHT)];
        contentView.scalesPageToFit = NO;
        
		[self.view addSubview:contentView];
		[self.view sendSubviewToBack:contentView];
		[contentView release];
        
        //
        // 数据文件夹路径
        NSString *cPath = [Common cacheFilePath:[Common keyWithURLString:url]];
        
        // 检测数据文件夹是否存在，如果不存在就创建
        if (![[NSFileManager defaultManager] fileExistsAtPath:cPath]){
           // NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
           // [contentView loadRequest:request];
        }else{
            NSData *data = [NSData dataWithContentsOfFile:cPath];
            NSDictionary *dict = (NSDictionary *)[data objectFromJSONData];
            
            dict = [dict objectForKey:docid];
            
            NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
            NSString *filePath = [resourcePath stringByAppendingPathComponent:@"content_template.html"];
            
            NSString *contentHtml = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
            contentHtml = [contentHtml stringByReplacingOccurrencesOfString:@"{{title}}" withString:[dict objectForKey:@"title"]];
            contentHtml = [contentHtml stringByReplacingOccurrencesOfString:@"{{ptime}}" withString:[dict objectForKey:@"ptime"]];
            contentHtml = [contentHtml stringByReplacingOccurrencesOfString:@"{{body}}" withString:[dict objectForKey:@"body"]];
            contentHtml = [contentHtml stringByReplacingOccurrencesOfString:@"{{source}}" withString:[dict objectForKey:@"source"]];
            
            [contentView loadHTMLString: contentHtml baseURL: [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
        }

    }
    return  self;
}



- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
